// 引入包
const Koa = require('koa')
const KoaRouter = require('koa-router')
// 引入获取请求体参数包
const bodyParser = require('koa-bodyparser')
// 实例化koa
const app = new Koa()
// 实例化koa-router
const router = new KoaRouter()
// 使用插件
app.use(bodyParser())
// 通过CORS解决跨域
// app.use(async (ctx, next) => {
// 	ctx.set("Access-Control-Allow-Origin", "*")
// 	await next()
//   })
// 使用所有路由及相关方法
app.use(router.routes()).use(router.allowedMethods())

// 首页数据
const indexData = require('./datas/index.json')
router.get('/', (ctx, next) => {
	ctx.body = {
		code: 200,
		indexData
	}
})
// it模块数据
const qianduan = require('./datas/itmoudel/qinaduan.json')
const houduan = require('./datas/itmoudel/houduanData.json')
const ai = require('./datas/itmoudel/ai.json')
const andriod = require('./datas/itmoudel/andriod.json')
const database = require('./datas/itmoudel/database.json')
const ios = require('./datas/itmoudel/ios.json')
const programDevelopment = require('./datas/itmoudel/programDevelopment.json')
router.get('/itmoudel/detailInfo', (ctx, next) => {
	let type = ctx.query.type
	if (type === '1' || type === undefined) {
		ctx.body = {
			code: 200,
			qianduan
		}
		return
	}
	if (type === '2') {
		ctx.body = {
			code: 200,
			houduan
		}
		return
	}
	if (type === '3') {
		ctx.body = {
			code: 200,
			andriod
		}
		return
	}
	if (type === '4') {
		ctx.body = {
			code: 200,
			ios
		}
		return
	}
	if (type === '5') {
		ctx.body = {
			code: 200,
			ai
		}
		return
	}
	if (type === '6') {
		ctx.body = {
			code: 200,
			database
		}
		return
	}
	if (type === '7') {
		ctx.body = {
			code: 200,
			programDevelopment
		}
		return
	}
	ctx.body = {
		code: 666,
		message: '404 Not Found!  路径警告！快点重新输入'
	}

})
// 获取导航列表
const navList = require('./datas/itmoudel/navList.json')
router.get('/itmoudel/navlist',(ctx, next) =>{
	ctx.body = {
		code:200,
		navList
	}
})
// 登录
router.post('/login', (ctx, next) => {
	let { userInfo, password } = ctx.request.body
	if (userInfo == 'admin' && password == '123') {
		ctx.body = {
			code: 200,
			message:'登录成功',
			userInf:{
				name:"简书钻首席小管家",
				image:"https://upload.jianshu.io/users/upload_avatars/14715425/e0668349-8c75-43db-8a9d-c388e5f00d0d.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240",
				gaunzhu:15,
				fensi:168340,
				wenzhang:449,
				zishu:457786,
				xihuan:112455,
				zichan:"29.1w"
			}
		}
	}else{
		ctx.body = {
			code: 444,
			message:'账号或密码错误'
		}
	}
	
})
// // 主页导航页面数据
// let indexCateList = require('./datas/indexCateList.json')
// router.get('/getindexCateList', async (ctx, next) => {
// 	// 生成的H5应用存在跨域问题，通过CORS设置跨域配置
// 	ctx.set('Access-Control-Allow-Origin', '*')
// 	await new Promise(resolve => setTimeout(() => resolve(),2000))
// 	ctx.body = indexCateList;
// });



// 提示信息
app.listen('3006', error => {
	if (error) {
		console.log('服务器启动失败,', error)
		return
	}
	console.log('服务器启动成功')
	console.log('服务器地址为：http://localhost:3006')
})